// pages/historyOrder/index.js
const time = require("../../utils/util.js");
let App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list:[],
    page:1,
    pagesize:10,
    allData:true,
    notData:false,
    down:false,
    timeArray: null,
    status:"1",
    return_visit_time: '请选择回访时间', // 回访时间 datetime格式
    month:['12','11','10'],
    startTime: '',
    endTime: '',
    tabList: [
      {
        title: '未处理',
        filterDate: '',
        page: 1,
        pagesize: 10,
        list: [],
      },
      {
        title: '待回访',
        filterDate: '',
        page: 1,
        pagesize: 10,
        list: [],
      },
      {
        title: '已完成',
        filterDate: '',
        page: 1,
        pagesize: 10,
        list: [],
      },
    ]
  },
  onChange(event) {
    let index = event.detail.index
    if(index == 0){
      this.setData({
        status:1,
        page:1
      })
    }else if(index == 1){
      this.setData({
        status:2,
        page:1
      })
    }else{
      this.setData({
        status:4,
        page:1
      })
    }
    let {tabList, active} = this.data
    active = event.detail.index
    const tab = tabList[active]
    tab.page = 1
    this.setData({
      tabList,
      active,
    })
    this.getList()
  },
  getList(){
    const {tabList, active} = this.data
    const tab = tabList[active]
    let data = {
      page:this.data.page,
      pagesize:tab.pagesize,
      order_status:this.data.status,
      date: tab.filterDate,
    }
    App._get(App.Domain+'Handler/Applet/historyOrder', data,  (result)=> {
      if(result.code==1){
        tab.list = result.data.list
        if(tab.list.length<10){
          this.setData({
            allData:false,
            notData:true
          })
        }
        tab.list.forEach(item=>{
          item.created_at = time.formatTime(item.created_at)
        })
        this.setData({
          tabList
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initDate()
    this.getList()
  },
  initDate() {
    let dateNow = new Date();
    let year = dateNow.getFullYear();
    let month = dateNow.getMonth() + 1;
    month = month < 10 ? ('0'+month) : month
    const endTime = `${year}-${month}`
    const startTime = `${year-1}-${month}`
    const tabList = this.data.tabList
    tabList.forEach(element => {
      element.filterDate = endTime
    });
    this.setData({
      tabList,
      currentDate: endTime,
      startTime,
      endTime,
    })
  },
  //日期选择
  selectDataTime(e) {
    const {tabList, active} = this.data
    const tab = tabList[active]
    tab.filterDate = e.detail.value
    this.setData({
      tabList,
    })
    this.getList()
  },
    //去处理
    getOrder(e){
      console.log(e)
      let id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../orderReturn/index?id='+id
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({refresh:true,allData:true,page:1, notData:false})
    this.getList()
    wx.showNavigationBarLoading() 
    setTimeout(()=>{
      this.getData = '数据拿到了'
      this.setData({refresh:false})
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    },500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    if(!this.data.allData){
      return
    }else{
      this.setData({
        down:true
      })
      let page = that.data.page
      page++
      this.setData({page:page})
      setTimeout(()=>{
        that.getList()
        this.getData = '数据拿到了'
        this.setData({down:false})
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
      },1000)
     
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})